//模型层 获取数据
import {
  HTTP
} from '../utils/http';

//继承使用HTTP工具里的ajax方法
class DetailModel extends HTTP {
  //获取商品详情信息 需要goodsid
  getGoodsDetail(gid) {
    //利用Promise里resolve写好的字符串和数据结合一起抛出到另外的模块
    return new Promise((resolve, reject) => {
      this.ajax({
        url: 'Shopping_cart/getGoodsDetail',
        type: 'POST',
        dataType: 'JSON',
        data: {
          id: gid
        },
        success: (data) => {
          const code = data.error_code;
          //没数据时返回主页
          code === '1001' && (window.location.href = 'index.html');

          //利用resolve方法把data放出去
          resolve(data);
        }
      })
    })
  }

  //更新购物车信息
  updateShoppingCart(gid) {
    return new Promise((resolve, reject) => {
      this.ajax({
        url: 'ShoppingCart/updateShoppingCart',
        type: 'POST',
        dataType: 'JSON',
        data: {
          uid: 1,
          goodsId: gid
        },
        success: (data) => {
          resolve(data.error_code);
        }
      })
    })
  }
}

//导出模块
export {
  DetailModel
};